﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Escape
{
    class Area
    {
        public int mapWidth, mapHeight;

        Floor floor;
        Ceiling ceiling;

        List<Block> blocks = new List<Block>();
        Texture2D mapTex;

        public Area(String textureName, String name)
        {
            mapTex = Basic.content.Load<Texture2D>(textureName);

            mapWidth = mapTex.Width;
            mapHeight = mapTex.Height;

            floor = new Floor(mapWidth, mapHeight);
            ceiling = new Ceiling(mapWidth, mapHeight);

            Vector3 spawnPoint = Vector3.Zero;

            Color[,] mapData = getData(mapTex);

            for (int x = 0; x < mapWidth; x++)
            {
                for (int y = 0; y < mapHeight; y++)
                {
                    if (mapData[x, y] == Color.White)
                        blocks.Add(new MovingBlock(new Vector3(x, 0, mapHeight - y - 1)));

                    if (mapData[x, y] == Color.Yellow)
                        spawnPoint = new Vector3(x + 0.5f, -0.5f, mapHeight - y - 1 + 0.5f);

                    if (mapData[x, y] == Color.Red)
                        blocks.Add(new VanishBlock(new Vector3(x, 0, mapHeight - y - 1)));

                    if (mapData[x, y] == new Color(100, 100, 100))
                        blocks.Add(new PitBlock(new Vector3(x, 0, mapHeight - y - 1)));
                }
            }

            for (int x = 0; x < mapWidth; x++)
            {
                for (int y = 0; y < mapHeight; y++)
                {
                    if (mapData[x, y] == new Color(0, 255, 0))
                        blocks.Add(new GridBlock(new Vector3(x, 0, mapHeight - y - 1), direction.z));
                    if (mapData[x, y] == new Color(255, 0, 255))
                        blocks.Add(new FistBlock(new Vector3(x, 0, mapHeight - y - 1), direction.z));
                }
            }

            GameScreen.c.CameraPosition = spawnPoint;
        }

        public Vector3 tryMove(Vector3 add, Vector3 old)
        {
            Vector3 result = old;
            if (isFree(result + new Vector3(add.X, 0, 0)))
                result.X += add.X;
            if (isFree(result + new Vector3(0, 0, add.Z)))
                result.Z += add.Z;
            return result;
        }

        public bool isFree(Vector3 v)
        {
            foreach (Block b in blocks)
            {
                if (b.collision && b.isInBlock(v))
                    return false;
            }

            return true;
        }

        public void Update()
        {
            foreach (Block b in blocks)
            {
                b.Update();
                b.use();
            }
        }

        public void Render()
        {

            floor.Render();
            ceiling.Render();

            foreach (Block b in blocks)
                b.isInBlock(GameScreen.c.CameraPosition);

            foreach (Block b in blocks)
                b.Render();
        }


        private Color[,] getData(Texture2D tex)
        {
            Color[] colors = new Color[tex.Width * tex.Height];
            tex.GetData(colors);

            Color[,] colors2D = new Color[tex.Width, tex.Height];

            for (int i = 0; i < tex.Width; i++)
            {
                for (int j = 0; j < tex.Height; j++)
                {
                    colors2D[i, j] = colors[i + j * tex.Width];
                }
            }

            return colors2D;
        }
    }
}
